/*
 *
 * author: fernado
 * create date: 2011-5-17
 */

package iceworld.fernado.file.impl;

import iceworld.fernado.consts.Constants;
import iceworld.fernado.entity.Field;
import iceworld.fernado.resource.SysConfig;
import iceworld.fernado.util.Utils;

import java.util.List;

public class CsharpSaveResource extends SaveResource {
	
	private static final String QUERY_OBJECT = "QueryObject";
	
	public String generateMainFrame(String clazzName, String tableComment, List<Field> fields) {
		StringBuilder result = new StringBuilder();
		result.append("using System;\r\n");
		result.append("using System.Collections.Generic;\r\n");
		result.append("using System.Linq;\r\n");
		result.append("using System.Text;\r\n\r\n");
		result.append("namespace ");
		result.append(SysConfig.getInstance().getValue("namespacename"));
		result.append("{");
		result.append("\r\n");
		
		addTableComment(result, tableComment);
		
		result.append(Utils.howMany4Spaces(1));
		result.append("public class ").append(clazzName);
		result.append("\r\n");
		result.append(Utils.howMany4Spaces(1));
		result.append("{");
		result.append("\r\n");
		
		for (Field field: fields) {
			generateContent(result, field);
		}

		result.append(Utils.howMany4Spaces(1));
		result.append("}");
		result.append("\r\n");
		result.append("\r\n");
		
		generateQueryObject(result, clazzName, tableComment, fields);
		
		result.append("}");
		
		return result.toString();
	}
	
	private void generateQueryObjectConstructor(String clazzName, StringBuilder builder) {
		builder.append(Utils.howMany4Spaces(2));
		builder.append("public ");
		builder.append(clazzName);
		builder.append("()");
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(2));
		builder.append("{");
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(3));
		builder.append("this.Reset();");
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(2));
		builder.append("}");
		builder.append("\r\n");
	}
	
	private void generateResetMethod(List<Field> fields, StringBuilder builder) {
		builder.append(Utils.howMany4Spaces(2));
		builder.append("public override void Reset()");
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(2));
		builder.append("{");
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(3));
		builder.append("base.Reset();");
		builder.append("\r\n");
		for (Field field: fields) {
			generateResetMethodContent(builder, field);
		}
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(2));
		builder.append("\r\n");
		builder.append("}");
	}
	
	private void generateResetMethodContent(StringBuilder builder, Field field) {
		if (!Utils.isEmpty(field.getType())) {
			String name = field.getName();
			builder.append(Utils.howMany4Spaces(3));
			builder.append("this.");
			builder.append(name);
			builder.append(" = ");
			builder.append("\"\";");
			builder.append("\r\n");
		}
	}
	
	public String generateQueryObject(StringBuilder builder, String clazzName, String tableComment, List<Field> fields) {
		String newClazzName = new StringBuilder().append(clazzName).append(QUERY_OBJECT).toString();
		builder.append(Utils.howMany4Spaces(1));
		builder.append("public class ").append(newClazzName)
			.append(Constants.EMPTY_STRING)
			.append(Constants.COLON)
			.append(Constants.EMPTY_STRING)
			.append(QUERY_OBJECT);
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(1));
		builder.append("{");
		builder.append("\r\n");
		
		generateQueryObjectConstructor(newClazzName, builder);
		
		for (Field field: fields) {
			generateContent(builder, field);
		}
		
		generateResetMethod(fields, builder);
		
		builder.append(Utils.howMany4Spaces(1));
		builder.append("}");
		builder.append("\r\n");
		builder.append("}");
		return builder.toString();
	}

	public String generateLanuageFrame(String clazzName, String tableComment, List<Field> fields) {
		String result = Constants.EMPTY_STRING;
		result = generateMainFrame(clazzName, tableComment, fields);
		return result;
	}

	@Override
	public String getLanguageSuffix() {
		return ".cs";
	}
}
